package com.loan.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.domain.entity.SysUser;
import com.loan.common.core.domain.model.LoginUser;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.helper.LoginHelper;
import com.loan.common.utils.StringUtils;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.ApplyProductEntity;
import com.loan.system.mapper.ApplyProductMapper;
import com.loan.system.mapper.SysUserMapper;
import com.loan.system.service.ApplyProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author shiguang on 2022/9/7 15:25
 */
@RequiredArgsConstructor
@Service
public class ApplyProductServiceImpl implements ApplyProductService {

    private final ApplyProductMapper applyProductMapper;

    private final SysUserMapper sysUserMapper;

    @Override
    public TableDataInfo<ApplyProductEntity> selectPageList(ApplyProductEntity entity, PageQuery pageQuery) {
        Page<ApplyProductEntity> page = applyProductMapper.selectPageProductList(pageQuery.build(), this.buildQueryWrapper(entity));
        return TableDataInfo.build(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addApplyProduct(ApplyProductEntity entity) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (ObjectUtil.isNotNull(loginUser.getCompanyId())){
            entity.setCompanyId(loginUser.getCompanyId());
        }
        return applyProductMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateApplyProduct(ApplyProductEntity entity) {
        return applyProductMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteApplyProductById(Long id) {
        return applyProductMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStatus(ApplyProductEntity entity) {
        return applyProductMapper.updateById(entity);
    }

    @Override
    public ApplyProductEntity selectApplyProductById(Long id) {
        return applyProductMapper.selectById(id);
    }

    @Override
    public List<ApplyProductEntity> selectList() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getCompanyId() != null) {
            return applyProductMapper.selectList(new QueryWrapper<ApplyProductEntity>().eq("company_id", loginUser.getCompanyId()).orderByAsc("id"));
        }else{
            return applyProductMapper.selectList(new QueryWrapper<ApplyProductEntity>().isNull("company_id").orderByAsc("id"));
        }
    }

    private Wrapper<ApplyProductEntity> buildQueryWrapper(ApplyProductEntity entity) {
        Map<String, Object> params = entity.getParams();
        QueryWrapper<ApplyProductEntity> wrapper = Wrappers.query();
        LoginUser loginUser = LoginHelper.getLoginUser();
//        if (ObjectUtil.isNotNull(entity.get))
        wrapper.eq(ObjectUtil.isNotNull(entity.getApplyCycle()), "ap.apply_cycle", entity.getApplyCycle())
                .eq(ObjectUtil.isNotNull(loginUser.getCompanyId()), "ap.company_id", loginUser.getCompanyId())
                .eq(StringUtils.isNotBlank(entity.getStatus()), "ap.status", entity.getStatus())
                .eq(ObjectUtil.isNotNull(entity.getCompanyId()), "ap.company_id", entity.getCompanyId())
                .eq("ap.del_flag", "0")
                .orderByDesc("ap.id");
        return wrapper;
    }
}
